package com.frank.controller;

import com.frank.dto.*;
import com.frank.feign.ResumeFeign;
import com.frank.model.JobRecord;
import com.frank.model.Resume;
import com.frank.service.ResumePoolService;
import com.frank.utils.RecruitProcessEnum;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/recruit")
public class ResumePoolController {

    @Resource
    private ResumePoolService resumePoolService;

    @Resource
    private ResumeFeign resumeFeign;


    @RequestMapping(value = "/getResumeListByType", method = RequestMethod.POST)
    public JsonResult<?> getResumeListByType(@RequestParam("recruitType") String recruitType, @RequestBody QueryPageDto queryPageDto) {
        CommonListDto commonListDto = resumePoolService.getResumeList(recruitType, queryPageDto);

        return new JsonResult<>(200,"获取简历列表成功",commonListDto);
    }

    @RequestMapping(value = "/getResumeByUserId", method = RequestMethod.GET)
    public JsonResult<?> getResumeByUserId(@RequestParam("userId") Integer userId) {
        JsonResult jsonResult = resumeFeign.getByUserId(userId);

        return new JsonResult<>(200,"获取简历记录成功",jsonResult.getData());
    }

    @Transactional
    @RequestMapping(value = "/updateResumeRecord", method = RequestMethod.POST)
    public JsonResult<?> updateResumeRecord(@RequestBody UpdateResumeDto updateResumeDto) {

        Resume resume = new Resume();
        resume.setId(updateResumeDto.getResumeId());
        resume.setRemark(updateResumeDto.getRemark());
        resume.setUserId(updateResumeDto.getUserId());

        JsonResult jsonResult = resumeFeign.updateResume(resume);

        JobRecord jobRecord = new JobRecord();
        jobRecord.setId(updateResumeDto.getJobRecordId());
        jobRecord.setProcessState(RecruitProcessEnum.getIndexByName(updateResumeDto.getProcess_state()));

        boolean result = resumePoolService.updateJobRecord(jobRecord);

        if (result && jsonResult.getStatus() == 200){
            return new JsonResult<>(200,"更新简历记录成功");
        }else {
            return new JsonResult<>(500,"更新简历记录失败");
        }
    }


    @Transactional
    @RequestMapping(value = "/deleteResumeRecord", method = RequestMethod.POST)
    public JsonResult<?> deleteResumeRecord(@RequestParam("jobRecordId") Integer jobRecordId) {
        if (resumePoolService.deleteJobRecord(jobRecordId)){
            return new JsonResult<>(200,"删除简历记录成功");
        }else {
            return new JsonResult<>(500,"删除简历记录失败");
        }
    }

    @Transactional
    @RequestMapping(value = "/submitJobWish", method = RequestMethod.POST)
    public JsonResult<?> submitJobWish(@RequestBody JobRecord jobRecord) {
        if (resumePoolService.submitJobWish(jobRecord)){
            return new JsonResult<>(200,"提交工作志愿成功");
        }else {
            return new JsonResult<>(500,"提交工作志愿失败");
        }
    }

    @RequestMapping(value = "/getJobRecordList", method = RequestMethod.GET)
    public JsonResult<?> getJobRecordList(@RequestParam("userId") Integer userId) {
        List<JobRecordDto> jobRecordDtoList = resumePoolService.getJobRecordByUserId(userId);

        return new JsonResult<>(200,"获取求职记录列表成功",jobRecordDtoList);
    }
}
